package Auction;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Set;






public class Graph_Auction {
	
	
	static int QUEUELENGTH = 10000;
	
	PriorityQueue<Left_node_auction> l_queue = new PriorityQueue<>(QUEUELENGTH, new lNodeComparator()); 
	PriorityQueue<Right_group_auction> r_queue = new PriorityQueue<>(QUEUELENGTH, new RNodeComparator()); 
	ArrayList<Left_node_auction> req = new ArrayList<>();
	ArrayList<Right_group_auction> help = new ArrayList<>();
	double[][] weight;
	Set<Left_node_auction> lset = new HashSet<>();
	Set<Right_group_auction> rSet = new HashSet<>();
	HashMap<Integer, Right_group_auction> rgroup_num = new HashMap<>();
	HashMap<String, Integer> rnode_in_group = new HashMap<>();
	
	public Graph_Auction() throws IOException{
		loadbid("src/Auction/bid.txt");
		inits();
	}

	
	public void loadbid(String file) throws IOException{
		BufferedReader br = new BufferedReader(new FileReader(file));
		String line;
		int flag = 0;
		
		while((line=br.readLine())!=null){
			
			String[] to_write = line.split(",");
			if(to_write[0].charAt(0)=='l'){
				Left_node_auction templ = new Left_node_auction(to_write[0], Double.parseDouble(to_write[1]));
				l_queue.add(templ);
				continue;
			}
			
			if(to_write[0].charAt(0)=='r' && to_write.length ==1){
				Right_group_auction temp_r_group = new Right_group_auction(flag);
				for(int i=0;i<to_write.length;i++){
					String tempname = to_write[i];
					temp_r_group.addname(tempname);
					rnode_in_group.put(tempname, flag);
				}
				rgroup_num.put(flag++, temp_r_group);
				r_queue.add(temp_r_group);
				continue;
			}
				
			if(to_write.length>1){
				if (to_write[0].charAt(0)=='r' && to_write[1].length()<3){

					Right_group_auction temp_r_group = new Right_group_auction(flag);
					for(int i=0;i<to_write.length;i++){
						String tempname = to_write[i];
						temp_r_group.addname(tempname);
						rnode_in_group.put(tempname, flag);
					}
					rgroup_num.put(flag++, temp_r_group);
					r_queue.add(temp_r_group);
				}
				if(to_write[0].charAt(0)=='r' && (to_write[1].toCharArray().length==3)){
					Integer index = rnode_in_group.get(to_write[0]);
					r_queue.remove(rgroup_num.get(index));
					rgroup_num.get(index).addprice(to_write[0], Double.parseDouble(to_write[1]));
					r_queue.add(rgroup_num.get(index));
				}
			}		
		}
		br.close();
		
		Right_group_auction rr = r_queue.poll();
		Left_node_auction ll = l_queue.poll();
		while(ll!=null){
			req.add(ll);
			lset.add(ll);
			ll=l_queue.poll();
		}
		while(rr!=null){
			help.add(rr);
			rSet.add(rr);
			rr=r_queue.poll();
		}
		for(int i=0;i<req.size();i++){
			req.get(i).setNumber(i);
		}
		for(int i=0;i<help.size();i++){
			help.get(i).setNumber(i);
		}
		
		l_queue.clear();
		r_queue.clear();
		rgroup_num.clear();
		rnode_in_group.clear();
		
	}
	
	
	//这里把图转化为weigh，double行的二维数组
	//并且向hashset里写入数据
	public void inits(){
		int n = Math.max(req.size(), help.size());
		weight = new double[n][n];
		for(int i = 0; i<n; ++i)
			for(int j=0;j<n;++j)
				weight[i][j] = -(1<<10);
		
		for(int i=0;i<req.size();i++){
			Left_node_auction templ = req.get(i);
			double lprice = templ.price;
			for(int j=0;j<help.size();j++){
				double rprice = help.get(j).getPrice();
				if(lprice>=rprice){
					weight[i][j] = Math.floor((lprice-rprice)*10+0.5)/10.0;
//					weight[i][j] = ((int)lprice*10-(int)rprice*10)/10.0;
				}
			}
			
		}
	}
	
	public class lNodeComparator implements Comparator<Left_node_auction> {
		public int compare(Left_node_auction x, Left_node_auction y) {
			if (x.price < y.price)
				return 1;
			if (x.price > y.price)
				return -1;
			return 0;
		}
	}
	
	public class RNodeComparator implements Comparator<Right_group_auction> {
		public int compare(Right_group_auction x, Right_group_auction y) {
			if (x.getPrice() < y.getPrice())
				return 1;
			if (x.getPrice() > y.getPrice())
				return -1;
			return 0;
		}
	}
	
	public static void main(String[] args) throws IOException{
	
		Graph_Auction test = new Graph_Auction();
		Iterator<Right_group_auction> test_i = test.help.iterator();
		while(test_i.hasNext()){
			System.out.println(test_i.next().getPrice());
		}
		for(int i=0;i<test.weight.length;i++){
			for(int j=0;j<test.weight.length;j++){
				System.out.print(test.weight[i][j]+" ");
			}
			System.out.println();
		}
		
	}
}
